package com.axzhengxin.platform.assistant.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.axzhengxin.platform.assistant.domain.BizMarketUser;
import com.axzhengxin.platform.assistant.mapper.BizMarketUserMapper;
import com.axzhengxin.platform.assistant.service.IBizMarketUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.logging.log4j.util.PropertySource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author axzhengxin
 * @date 2021-03-08
 */
@Service
public class BizMarketUserServiceImpl implements IBizMarketUserService {
    @Autowired
    private BizMarketUserMapper bizMarketUserMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public BizMarketUser selectBizMarketUserById(Long id) {
        return bizMarketUserMapper.selectBizMarketUserById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param bizMarketUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BizMarketUser> selectBizMarketUserList(BizMarketUser bizMarketUser) {
        return bizMarketUserMapper.selectBizMarketUserList(bizMarketUser);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param bizMarketUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBizMarketUser(BizMarketUser bizMarketUser) {
        return bizMarketUserMapper.insertBizMarketUser(bizMarketUser);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param bizMarketUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBizMarketUser(BizMarketUser bizMarketUser) {
        return bizMarketUserMapper.updateBizMarketUser(bizMarketUser);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteBizMarketUserByIds(Long[] ids) {
        return bizMarketUserMapper.deleteBizMarketUserByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteBizMarketUserById(Long id) {
        return bizMarketUserMapper.deleteBizMarketUserById(id);
    }

    @Override
    public Integer batchSave(String[] reqIds, String[] userIds) {
        Integer a = 1;
        List<SysUser> userList = new ArrayList<SysUser>();
        Arrays.asList(userIds).forEach(userid -> {
            SysUser sysUser = new SysUser();
            sysUser.setUserId(Long.parseLong(userid));
            SysUser sysUser1 = sysUserMapper.selectUserById(Long.parseLong(userid));
            sysUser.setDeptId(sysUser1.getDeptId());
            userList.add(sysUser);
        });
        List<BizMarketUser> list = new ArrayList<BizMarketUser>();
        Arrays.asList(reqIds).forEach(reqId -> {
            userList.forEach(user -> {
                BizMarketUser bizMarketUser = new BizMarketUser();
                bizMarketUser.setMarketStatus("1");
                bizMarketUser.setMarketUser(user.getUserId());
                bizMarketUser.setMarketUserDept(user.getDeptId());
                bizMarketUser.setMarketId(Long.parseLong(reqId));
                list.add(bizMarketUser);
            });
        });
        //去重
        List<BizMarketUser> bizMarketUsers = bizMarketUserMapper.selectBizMarketUserListInfo();
        List<BizMarketUser> newList = list.stream()
                .filter(u -> {
                    return bizMarketUsers.stream()
                            .noneMatch(biz -> biz.getMarketUser().equals(u.getMarketUser()) && biz.getMarketId().equals(u.getMarketId()));
                })
                .collect(Collectors.toList());
        if (null != newList && newList.size() > 0) {
            a = bizMarketUserMapper.batchSave(newList);
        }
        return a;
    }
}
